package controller;

import java.sql.SQLException;
import java.util.ArrayList;

import db.DBConnection;
import db.DBSubContractor;
import db.DBSubContractorJob;
import db.IFSubContractor;
import db.IFSubContractorJob;
import model.SubContractor;
import model.SubContractorJob;

public class SubContractorController extends OutsiderController
{
	private static SubContractorController instance = null;

	public static SubContractorController getInstance()
	{
		if(instance == null)
			instance = new SubContractorController();

		return instance;
	}

	public SubContractorController()
	{

	}

	public SubContractor searchSubContractorID(int subContractorID) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorID(subContractorID);
	}

	public SubContractor searchSubContractorByName(String name) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorByName(name);
	}

	public ArrayList<SubContractor> searchSubContractorAll() throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorAll();
	}

	public ArrayList<SubContractor> searchSubContractorEverything(boolean active, String jobName, String search) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorEverything(active, jobName, search);
	}

	public ArrayList<SubContractor> searchSubContractorName(boolean active, String jobName, String search) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorName(active, jobName, search);
	}

	public ArrayList<SubContractor> searchSubContractorEmail(boolean active, String jobName, String search) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorEmail(active, jobName, search);
	}

	public ArrayList<SubContractor> searchSubContractorPhoneNo(boolean active, String jobName, String search) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorPhoneNo(active, jobName, search);
	}

	public ArrayList<SubContractor> searchSubContractorCity(boolean active, String jobName, String search) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorCity(active, jobName, search);
	}

	public ArrayList<SubContractor> searchSubContractorCountry(boolean active, String jobName, String search) throws SQLException
	{
		IFSubContractor dbSubContractor = new DBSubContractor();

		return dbSubContractor.searchSubContractorCountry(active, jobName, search);
	}

	/*
	 * 
	 * SubConractorJob
	 * 
	 */

	public int createSubContractorJob(String jobName) throws SQLException
	{
		int result = -3;

		try 
		{		
			SubContractorJob newSubContractorJob = new SubContractorJob();

			newSubContractorJob.setJobName(jobName);

			DBConnection.startTransaction();

			IFSubContractorJob dbSubContractorJob = new DBSubContractorJob();
			result = dbSubContractorJob.createSubContractorJob(newSubContractorJob);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateSubContractorJob(int jobID, String jobName) throws SQLException
	{
		int result = -3;

		try 
		{		
			SubContractorJob newSubContractorJob = new SubContractorJob();

			newSubContractorJob.setJobID(jobID);
			newSubContractorJob.setJobName(jobName);

			DBConnection.startTransaction();

			IFSubContractorJob dbSubContractorJob = new DBSubContractorJob();
			result = dbSubContractorJob.updateSubContractorJob(newSubContractorJob);

			DBConnection.commitTransaction();
		}
		catch (Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public ArrayList<SubContractorJob> printAllSubContractorJobs() throws SQLException
	{
		IFSubContractorJob dbSubContractorJob = new DBSubContractorJob();

		return dbSubContractorJob.printAllSubContractorJobs();
	}

	public SubContractorJob findSubContractorJobByName(String jobName) throws SQLException
	{
		IFSubContractorJob dbSubContractorJob = new DBSubContractorJob();

		return dbSubContractorJob.findSubContractorJobByName(jobName);
	}

	public int findSubcontractorJobCount(int jobID) throws SQLException
	{
		IFSubContractorJob dbSubContractorJob = new DBSubContractorJob();

		return dbSubContractorJob.findSubcontractorJobCount(jobID);
	}
}